/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pgentity.events.release_event;

import config.PGConfig;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import logging.report.UserAction;
import pgentity.EntityContext;
import pgentity.prize.EggsPrize;
import pgentity.prize.IPrizeFactory;
import pgentity.prize.PGPrize;
import share.PGHelper;

/**
 *
 * @author Salm
 */
public class REItemsPrize implements PGPrize {
    private final Map<String, Number> items;
    
    private REItemsPrize(Map<String, Number> data)
    {
        items = data;
    }
    
    @Override
    public Map<String, Object> award(EntityContext context, long now)
    {
        REUser user = REUser.getUser(context.getUser());
        user.inv().addEggs(items);
        return Collections.EMPTY_MAP;
    }

    @Override
    public boolean canPrize(EntityContext context, long now)
    {
        return PGConfig.inst().releaseEvent().inEvent(now);
    }
    
    public static class Factory implements IPrizeFactory
    {
        private Factory()
        {
            super();
        }
        
        private static final Factory inst = new Factory();
        
        public static Factory inst()
        {
            return inst;
        }
        
        @Override
        public PGPrize createPrize(Object prizeData, UserAction src) {
            Map<String, Object> mData = (Map) prizeData;
            Map<String, Number> items = new HashMap();
            
            for (Map.Entry<String, Object> needEggEntry : mData.entrySet()) {
                String itemKind = needEggEntry.getKey();
                int nEgg = PGHelper.toInt(needEggEntry.getValue());
                
                items.put(itemKind, nEgg);
            }
            
            return new REItemsPrize(items);
        }
    }
}
